package com.example.community.controller;



import com.example.community.dao.pojo.DiscussPost;
import com.example.community.dao.pojo.Page;
import com.example.community.dao.pojo.User;

import com.example.community.service.DiscussPostService;
import com.example.community.service.LikeService;
import com.example.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.community.uitls.CommunityConstant.ENTITY_TYPE_POST;

/**
 * @ClassName: IndexController
 * @Description:
 * @Author
 * @Date 2022-03-15
 * @Version 1.0
 */
@Controller
//@RequestMapping("community")
public class IndexController {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    @GetMapping("/denied")
    public String denied(){
        return "/error/404";
    }

    @GetMapping("index")
    public String getIndexPage(Model model, Page page) {
        // 方法调用前,SpringMVC会自动实例化Model和Page,并将Page注入Model.
        // 所以,在thymeleaf中可以直接访问Page对象中的数据.
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index");

        List<DiscussPost> list = discussPostService.selectDiscussPosts(0, page.getOffset(), page.getLimit());
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null) {
            for (DiscussPost post : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                User user = userService.findUserById(post.getUserId());
                map.put("user", user);

                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        return "/index";
    }
//    @GetMapping("index")
//    public String getIndexPage(@RequestParam(name="page",required = true,defaultValue = "1") Integer page, Model model){
//        List<DpU> dpUList = new ArrayList<>();
//
//        List<Map<String, Object>> discussPosts = new ArrayList<>();
//        Page<DiscussPost> postPage = discussPostService.getDiscussPostsPage(page);
//        if (postPage!=null){
//            for (DiscussPost post : postPage.getRecords()){
//                Map<String, Object> map = new HashMap<>();
//                map.put("post", post);
//                User user = userService.findUserById(post.getUserId());
//                map.put("user", user);
//                discussPosts.add(map);
//            }
//        }
//
//        model.addAttribute("page",postPage);
//        model.addAttribute("discussPosts", discussPosts);
//        return "/index";
//    }

}
